package com.dbky.alg.core.validate.code.processor;

import com.dbky.alg.core.validate.code.ImageCode;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.io.IOException;

/**
 * @Auther: tianchao
 * @Date: 2020/6/25 19:19
 * @Description:
 */
@Component("imageValidateCodeProcessor")
public class ImageCodeProcessor extends AbstractValidateCodeProcessor<ImageCode> {
    /**
     * 发送图形验证码 由子类实现
     *
     * @param request
     * @param validateCode
     */
    @Override
    protected void send(ServletWebRequest request, ImageCode validateCode) throws IOException {
        ServletOutputStream out = request.getResponse().getOutputStream();
        ImageIO.write(validateCode.getImage(), "JPEG", out);
        IOUtils.closeQuietly(out);
    }
}
